﻿Imports 地图商家采集系统

Public Class CMapGaoDe
    Inherits CMap
    Implements IMap
    Public Sub New(mapTask As Model_Task, callback As Action(Of CProductInfo))
        task = mapTask
        Me.UpdateView = callback
    End Sub
#Region "高德采集方法"
    Public Sub Deal() Implements IMap.Deal
        Log($"{MapMode.GaoDe.GetDescription}信息解析中...")
        Dim areas = task.Areas
        For Each area In areas
            Dim code = GetCode(area.City)
            GetProducts(code, area.City)
            ''通过城市码得到HTML,并获取列表页相关信息类BaiDuListInfo
            'GetBaiduProducts(code, area.City)
        Next
    End Sub
    'Private Function HasFilterKeyword(k As CKeyword) As Boolean
    '    If Not task.FilterKeywords.Count = 0 Then
    '        For Each ff In task.FilterKeywords
    '            If k.ToString = ff.ToString Then
    '                Return True
    '            End If
    '        Next
    '    End If
    '    Return False
    'End Function
    Private Sub GetProducts(code As String, city As String)
        Dim keywords = task.KeyWords
        For Each k As CKeyword In keywords
            Dim FlagPageEnd As Boolean = False, Page = -1
            Dim infoCount As Integer = 0
            Dim prePage As String = ""
            Dim success As Integer = 0
            Do
                success = 0
                Page += 1
                Dim cityName = Uri.EscapeDataString(city)
                Dim kw As String = Uri.EscapeDataString(k.Keyword)
                'https://www.amap.com/service/poiInfo?query_type=TQUERY&pagesize=20&pagenum=1&qii=true&cluster_state=5&need_utd=true&utd_sceneid=1000&div=PC1000&addr_poi_merge=true&is_classify=true&zoom=12&city=320100&geoobj=118.528727%7C31.906988%7C119.023138%7C32.127652&keywords=%E8%8B%B9%E6%9E%9C 
                Dim url = $"https://m.amap.com/service/poi/keywords.json?pagenum={Page}&user_loc=undefined&geoobj=&city={code}&keywords={kw}&cluster_state=5&client_network_class=4"
                '$"https://www.amap.com/service/poiInfo?query_type=TQUERY&pagesize=20&pagenum={Page}&qii=true&zoom=10&city={code}&keywords={kw}"
                Dim count As Integer = 1
                Dim html As String = ""
                Dim exp As Exception
                Do
                    exp = Nothing
                    html = Sy.Text.Encode.RemoveEscape_HTML(
                        Sy.Text.Encode.RemoveEscape_Java(Sy.Net.Factory.Http(url).SetCookies(cookies).SetUserAgent("Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Mobile Safari/537.36").SetReferer($"https://m.amap.com/search/view/keywords={kw}").SetExceptionHandle(Function() exp).Get()))
                    '.GetHtml(url, Exception:=exp, Cookies:=cookies)
                    count += 1
                Loop While exp IsNot Nothing And count < 5

                If exp Is Nothing Then
                    Dim arrSpliter = """rating""" '"com_flag"
                    If html.Length < 0 Or Not html.Contains(arrSpliter) Or prePage = html Then
                        Log("关键字：" & k.Keyword & "未采集到信息,请确认")
                        FlagPageEnd = True
                    Else
                        prePage = html
#Region "采集列表页信息"
                        Dim items = html.Split({arrSpliter}, StringSplitOptions.RemoveEmptyEntries)
                        For i = 1 To items.Count - 1
                            Dim item = items(i)
                            Dim address As String = ""
                            Dim workplane As String = ""
                            Dim title As String = ""
                            Dim phone As String = ""
                            Dim mobile As String = ""
                            Dim x As String = 0
                            Dim y As String = 0
#Region "地址"
                            Dim addrSpliter = """address"":"""
                            If item.Contains(addrSpliter) Then address = Split(item, addrSpliter)(1).Split({""""}, StringSplitOptions.None)(0)
#End Region
#Region "坐标"
                            Dim xSpliter = """longitude"":"
                            Dim ySpliter = """latitude"":"
                            If item.Contains(xSpliter) Then x = Split(item, xSpliter)(1).Split({",", "}"}, StringSplitOptions.None)(0)
                            If item.Contains(ySpliter) Then y = Split(item, ySpliter)(1).Split({",", "}"}, StringSplitOptions.None)(0)
                            workplane = "X:" & x & "|Y:" & y
#End Region
#Region "标题"
                            Dim titleSpliter = """name"":"""
                            If item.Contains(titleSpliter) Then title = Split(Split(item, titleSpliter)(1), """")(0)
#End Region

#Region "手机号"
                            Dim phoneSpliter = """phone"":"""
                            If item.Contains(phoneSpliter) Then mobile = Split(item, phoneSpliter)(1).Split({""""}, StringSplitOptions.None)(0).Trim
                            '座机号
                            Dim telSpliter = """tel"":"""
                            If item.Contains(telSpliter) Then phone = Split(item, telSpliter)(1).Split({""""}, StringSplitOptions.None)(0).Trim
#End Region
                            'Dim pidSpilter = "primary_uid"":"""
                            'Dim uidSpliter = """uid"":"""
#Region "访问详情页所需ID(备用，目前不需要,使用时，关闭注释即可)"
                            'Dim pid = ""
                            'Dim uid = ""
                            'If item.Contains(pidSpilter) Then
                            '    Dim temp = Split(item, pidSpilter)(1)
                            '    pid = Split(temp, """")(0)
                            '    If temp.Contains(uidSpliter) Then uid = Split(Split(temp, uidSpliter)(1), """")(0)
                            'End If
#End Region
                            'Debug.Print(address, workplane, phone, mobile)
#Region "采集过滤,添加"
#End Region
                            If FilterResult(title, phone, mobile, address) AndAlso FilterKeywordResult(title, task.FilterKeywords) Then
                                Dim info = New CProductInfo(address, title, workplane, city, k.Keyword, MapMode.GaoDe.GetDescription, url, phone, mobile)
                                success += If(info.Add(), 1, 0)
                                UpdateView(info)
                            End If
                        Next
#End Region
                        If items.Count <= 1 Then FlagPageEnd = True
                        If success = 0 Then FlagPageEnd = True
                    End If
                Else
                    Log("采集超时异常")
                    FlagPageEnd = True
                End If
            Loop Until FlagPageEnd
        Next
    End Sub
#End Region
End Class
